<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="查询类别" prop="stat">
              <el-select v-model="queryParams.stat" placeholder="请选择" clearable       @keyup.enter.native="handleQuery" >
                              <el-option label="请选择" value=""></el-option>
                              <el-option label="线路名称" value="1"></el-option>
                                    <el-option label="运营时间" value="2"></el-option>
                                    <el-option label="起点站名" value="3"></el-option>
                                    <el-option label="终点站名" value="4"></el-option>
                                    <el-option label="发车间隔" value="5"></el-option>
                                    <el-option label="票价" value="6"></el-option>
                            </el-select>
            </el-form-item>
      <el-form-item label="" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="lineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="状态" align="center"  >正常</el-table-column>
      <el-table-column label="线路名称" align="center" prop="name" />
      <el-table-column label="序号" align="center" prop="priority" />
      <el-table-column label="夏日运营时间" align="center" prop="summerTime" />
      <el-table-column label="冬日运营时间" align="center" prop="winter" />
      <el-table-column label="发车间隔" align="center" prop="rangeTime" />
      <el-table-column label="票价" align="center" prop="price" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始站点" align="center" prop="begName" />
      <el-table-column label="结束站点" align="center" prop="endName" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改线路对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="状态 1正常 0停用" prop="stat">
          <el-input v-model="form.stat" placeholder="请输入状态 1正常 0停用" />
        </el-form-item>
        <el-form-item label="线路名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入线路名称" />
        </el-form-item>
        <el-form-item label="序号" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="夏日运营时间" prop="summerTime">
          <el-input v-model="form.summerTime" placeholder="请输入夏日运营时间" />
        </el-form-item>
        <el-form-item label="冬日运营时间" prop="winter">
          <el-input v-model="form.winter" placeholder="请输入冬日运营时间" />
        </el-form-item>
        <el-form-item label="发车间隔" prop="rangeTime">
          <el-input v-model="form.rangeTime" placeholder="请输入发车间隔" />
        </el-form-item>
        <el-form-item label="票价" prop="price">
          <el-input v-model="form.price" placeholder="请输入票价" />
        </el-form-item>
        <el-form-item label="开始站点" prop="begName">
          <el-input v-model="form.begName" placeholder="请输入开始站点" />
        </el-form-item>
        <el-form-item label="结束站点" prop="endName">
          <el-input v-model="form.endName" placeholder="请输入结束站点" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLine, getLine, delLine, addLine, updateLine } from "@/api/system/line";

export default {
  name: "Line",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线路表格数据
      lineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stat: null,
        name: null,
        priority: null,
        summerTime: null,
        winter: null,
        rangeTime: null,
        price: null,
        createTime: null,
        begName: null,
        endName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询线路列表 */
    getList() {
      this.loading = true;
      listLine(this.queryParams).then(response => {
        this.lineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stat: null,
        name: null,
        priority: null,
        summerTime: null,
        winter: null,
        rangeTime: null,
        price: null,
        createTime: null,
        begName: null,
        endName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线路";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改线路";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除当前数据？').then(function() {
        return delLine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/line/export', {
        ...this.queryParams
      }, `line_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
